package com.jsg.sl651.entity.data;

import com.jsg.sl651.constant.EncodingElementIdentifier;
import com.jsg.sl651.util.ByteUtil;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.Arrays;

/**
 * 数据项解析内容
 * @param <T> 数据项类型
 */
@Getter
public class Element<T> {
    private final String asciiKey;
    private final String hexKey;
    // 原始数据
    private final byte[] originalMessage;
    // 解析后的数据
    private final T data;
    // 数据名
    private final String name;
    // 数据表述
    private final String desc;
    // 数据精度 [数据定义精度]
    private final int precision;
    // 数据单位
    private final String unit;

    public Element(byte[] originalMessage, T data, EncodingElementIdentifier.EncodingElement element) {
        this.originalMessage = originalMessage;
        this.data = data;
        this.name = element.getName();
        this.desc = element.getDesc();
        this.precision = element.getPrecision();
        this.unit = element.getUnit();
        this.hexKey = element.getHex().getHex();
        this.asciiKey = element.getAscii().getHex();
    }

    @Override
    public String toString() {
        return "数据项{" +
                "原始报文=" + ByteUtil.toHex(originalMessage) + "," +
                "数据=" + data + "," +
                "数据名=" + name + "," +
                "数据表述=" + desc + "," +
                "数据精度=" + precision + "," +
                "数据单位=" + unit +
                "}";
    }
}
